<template>
  <div>
    <p v-for="i in paramslist" :key="i">
      <van-field
        v-if="i.type == 'input'"
        :label="i.lable"
        v-model="values[i.lable]"
        class="tname"
      />
      <select v-if="i.type == 'select'" v-model="values[i.lable]">
        <option :value="o.type" v-for="o in i.values" :key="o">
          {{ o.name }}
        </option>
      </select>
      <van-uploader v-if="i.type == 'file'" />
      <el-date-picker
        v-if="i.type == 'datetime'"
        v-model="value1"
        type="datetimerange"
        range-separator="To"
        start-placeholder="开始时间"
        end-placeholder="结束时间"
        value-format="YYYY-MM-DD HH:mm:ss"
      >
      </el-date-picker>
    </p>
    <van-button @click="getaudit">选择审批人</van-button>

    <el-dialog v-model="dialogTableVisible" title="Shipping address">
      <el-tree
        :data="data"
        show-checkbox
        :node-key="id"
        :props="defaultProps"
        @check="getids"
      />
    </el-dialog>
    <van-button @click="submit">提交</van-button>
  </div>

</template>

<script>
import axios from "axios";
export default {
  name: "createtask",
  data() {
    return {
      // wid: this.$route.query.id,
      wid: 1,
      workf: {},
      paramslist: [],
      value1: [],
      values: {},
      pic: "1.jpg",
      checkdvalues: [],
      dialogTableVisible: false,
      data: [
        {
          id: 1,
          label: "Level one 1",
          children: [
            {
              id: 4,
              label: "Level two 1-1",
              children: [
                {
                  id: 9,
                  label: "Level three 1-1-1",
                },
                {
                  id: 10,
                  label: "Level three 1-1-2",
                },
              ],
            },
          ],
        },
        {
          id: 2,
          label: "Level one 2",
          children: [
            {
              id: 5,
              label: "Level two 2-1",
            },
            {
              id: 6,
              label: "Level two 2-2",
            },
          ],
        },
        {
          id: 3,
          label: "Level one 3",
          children: [
            {
              id: 7,
              label: "Level two 3-1",
            },
            {
              id: 8,
              label: "Level two 3-2",
            },
          ],
        },
      ],
      defaultProps: {
        children: "children",
        label: "name",
      },
      list: [],
    };
  },
  methods: {
    getwork() {
      axios({ url: "/getworkf", method: "get", params: { id: this.wid } }).then(
        (res) => {
          this.workf = res.data.workf;
          this.paramslist = JSON.parse(res.data.workf.params);
        }
      );
    },
    getlabelByType(type) {
      let label = "";
      for (var i = 0; i < this.paramslist.length; i++) {
        if (this.paramslist[i]["type"] == type) {
          label = this.paramslist[i]["lable"];
        }
      }
      return label;
    },
    getaudit() {
      //读取公司部门角色列表
      axios({ url: "/getcomanyuser", method: "get" }).then((res) => {
        this.data = res.data.list;
      });
      this.dialogTableVisible = true;
    },

    getids(val1, status) {
      // console.log(status);
      // console.log("getids中11:", val1);
      // console.log("getids中:", val1["id"]);
      this.checkdvalues = [];
      let list = status["checkedNodes"];
      for (var i = 0; i < list.length; i++) {
        if (list[i]["type"] == 3) {
          this.checkdvalues.push(list[i]["id"]);

          alert(this.checkdvalues);
        }
      }
    },
    submit() {
      if (this.pic) {
        let label = this.getlabelByType("file");
        this.values[label] = this.pic;
      }
      if (this.value1.length > 0) {
        let stime = this.getlabelByType("datetime");
        this.values[stime] = this.value1[0];
        this.values["结束时间"] = this.value1[1];
      }
      //获取第一个input框中的信息
      let name = document.getElementsByTagName("input")[0].value;
      let title = this.workf["name"] + name;
      axios({
        url: "/taskm",
        method: "post",
        data: {
          title: title,
          params: JSON.stringify(this.values),
          auditids: this.checkdvalues,
        },
      }).then((res) => {
        console.log(res);
      });
    },
  },
  mounted() {
    this.getwork();
  },
};
</script>

<style scoped></style>
